<!-- 编辑弹窗 -->
<template>
    <ele-modal
      width="30%"
      :movable="false"
      :visible="visible"
      :close-on-click-modal="true"
      custom-class="ele-dialog-form"
      :title="isUpdate ? '修改供应商信息' : '添加供应商信息'"
      @update:visible="updateVisible"
    >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-card  style="margin-top: -10px">        
          <el-row :gutter="24">
            <el-col :md="24" :sm="24">
                <el-form-item label="供应商编号:" prop="code">
                  <el-input v-model="form.code" disabled placeholder="系统自动生成"   >
                  </el-input>
                </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24">
              <el-form-item label="供应商名称:" prop="name">
                <el-input v-model="form.name" placeholder="请输入供应商名称"   >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="类型:" prop="typeId">
                <el-select  v-model="form.typeId" filterable clearable placeholder="请选择类型" style="width:100%" >
                  <el-option v-for="(item, index) in GSTypeDatas" :key="index" :label="item.Value" :value="item.Key" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="结算币种:" prop="currencyId">
                <el-select  v-model="form.currencyId" filterable clearable placeholder="请选择结算币种" style="width:100%" >
                  <el-option v-for="item in BZDatas" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="联系人:" >
                <el-input v-model="form.contactName" placeholder="请输入联系人"   />
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="联系电话:" >
                <el-input v-model="form.contactPhone" placeholder="请输入联系电话"   />
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="联系地址:">
                <el-input v-model="form.contactAddress" placeholder="请输入联系地址" type="number"   />
              </el-form-item>
            </el-col>
            <el-col :md="24" :sm="24" >
              <el-form-item label="备注:">
                <el-input v-model="form.remark" placeholder="请输入备注"  :rows="4" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" v-show="this.btnIndex!=3" :loading="loading" @click="save" v-permission="'Basic_Supplier_Save'">
        保存
      </el-button>
    </div>
    </ele-modal>
  </template>
  <script>

   import { QueryThirdcompanyType } from '@/api/enummanger';
   import { QueryAllChildDictViewByCode } from '@/api/systemmanager/dictmanager';
   import { AddSupplier, UpdateSupplier,GetSupplierById } from '@/api/basicdatamanager/supplier';
   import { isIntegerOrDecimal2 } from '@/utils/validate';

    export default {
      name: 'SupplierEdit',
      props: {
        // 弹窗是否打开
        visible: Boolean,
        // 修改回显的数据
        data: Object,
        // 按钮索引
        btnIndex:String,
      },
      data() {
        return {
          // 表单数据
          form: {},
          // 表单验证规则
          rules: {
            name: [{required: true,message: '请输入供应商名称',trigger: 'blur'}],
            typeId: [{required: true,message: '请输入供应商类型',trigger: 'blur'}],
            currencyId:[{required: true,message: '请输入结算币种',trigger: 'blur'}],
          },
          //单位
          GSTypeDatas:[],
      
          // 提交状态
          loading: false,
          // 是否是修改
          isUpdate: false,
          BZDatas:[],
        };
      },

      //初始化数据
      mounted(){
        this.queryStatusEnums();
      },
      methods: {
  
        /* 保存编辑 */
        save() {
          //校验必填信息
          this.$refs['form'].validate((valid) => {
            if (!valid) {return false;}

            //品牌Id 单位Id
            if(this.form.unitId && this.form.unitId==this.form.unitDesc) this.form.unitId = '';

            this.loading = true;
            const saveOrUpdate = this.isUpdate ? UpdateSupplier : AddSupplier;
            saveOrUpdate(this.form)
              .then((msg) => {
                this.loading = false;
                this.$message.success(msg);
                this.updateVisible(false);
                //回调父组件方法
                this.$emit('done');
              })
              .catch((e) => {
                this.loading = false;
                this.$message.error(e.message);
              });
          });
        },

        setData(id){
          GetSupplierById(id)
            .then((res) => {
              this.form = res; 
        });
      },

      
      /* 系统状态 */
      async queryStatusEnums(){
        let datas = await QueryThirdcompanyType();
        datas.map(e=>{
          if(e.Key !=5){
            this.GSTypeDatas.push({Key:e.Key,Value:e.Value});
          }
        });

        QueryAllChildDictViewByCode(this.Constant.C_HB).then((data)=>{
            this.BZDatas = [];
            this.BZDatas = data;
        });
      },

      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
        //更新文件列表对像
      },
     
    },
     
     
      /* 监听visible属性变化 */
      watch: {
        visible(visible) {
          if (visible) {
            if (this.data && this.data.id) {
              //赋值this.form
              this.form = {};
              this.setData(this.data.id);
              this.isUpdate = this.data.id !=null && this.data.id !=undefined;
            } else {
              this.form={};
              this.form.detail = ''
              this.isUpdate = false;
            }
          } else {
              this.$refs['form'].clearValidate();
              this.form = {};
          }
        }
      }
    };
  </script>
    



 @/api/basicdatamanager/customer